import { request } from '../utils/request.js'
import store from '../store'
import { Toast, Jump } from '../utils/tools.js'
import md5 from 'js-md5'
import { balance, login } from '@/api/Users.js'



//创建订单
export async function CreateOrder(params) {
	try {
		let res = await request({
			url: '/Order/CreateOrder',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}

//创建订单
export async function CreateOrderWards2(params) {
	try {
		let res = await request({
			url: '/Order/CreateOrderWards2',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}

export async function CreateOrder1(params) {
	try {
		let res = await request({
			url: '/Order/CreateOrder_1',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}


//购物车创建订单
export async function CreateOrder_Car(params, data) {
	try {
		let res = await request({
			url: '/Order/CreateOrder_Car',
			method: "post",
			params,
			data
		})
		return res
	} catch (e) {
		return false
	}
}

export async function First1(params) {
	try {
		let res = await request({
			url: '/Order/First_1',
			method: "post",
			params
		})
		return res
	} catch (e) {

		return false
	}
}

export async function First(params) {
	try {
		let res = await request({
			url: '/Order/First',
			method: "post",
			params
		})
		const { success, data } = res
		if (success) {
			data.expArr = []
			data.expLength = 0
			let expArr = [];
			if (data.expCode) {
				expArr = data.expCode.match(/\b(?:[A-Za-z]+\d+|\d+)\b/g) || []
				console.log(expArr)
				data.expLength = expArr.length;
				if (expArr.length > 1) {
					data.expArr = expArr || []
				} else {
					data.expCode = expArr[0]
				}
			}
		}

		return res
	} catch (e) {
		console.log('First', e)
		return false
	}
}


//继续支付
export async function CreatePayOrder(params, data) {
	try {
		let res = await request({
			url: '/Order/CreatePayOrder',
			method: "post",
			params,
			data
		})
		return res
	} catch (e) {
		console.log('e=====>', e)
		return false
	}
}
//合并支付
export async function CreatePayOrder_1(params) {
	try {
		let res = await request({
			url: '/Order/CreatePayOrder_1',
			method: "post",
			params,
		})
		return res
	} catch (e) {
		console.log('e=====>', e)
		return false
	}
}

//订单列表
export async function GetOrderPage(params) {
	try {
		let res = await request({
			url: '/Order/GetOrderPage',
			method: "post",
			params,
			isLogin: false,
		})
		const { success, list } = res
		if (success) {
			list.forEach(item => {
				const imageArr = item.picurl.split(',')
				item.picurLength = imageArr.length
				item.picurlList = imageArr
			})
		}

		return res
	} catch (e) {
		return false
	}
}

//签收
export async function SignIn(params) {
	try {
		let res = await request({
			url: '/Order/SignIn',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}

//评论
export async function Comment(params) {
	try {
		let res = await request({
			url: '/Goods/Comment/Comment',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}

// 设置审订单地址
export async function SetAddr(params) {
	try {
		let res = await request({
			url: '/Order/SetAddr',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}

// 取消订单
export async function CancellOrder(params) {
	try {
		let res = await request({
			url: '/Order/CancellOrder',
			method: "post",
			params
		})
		return res
	} catch (e) {
		console.log(e)
		return false
	}
}

// 售后
export async function AftersellOrderApply(params) {
	try {
		let res = await request({
			url: '/Order/AftersellOrderApply',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}



//订单 数量统计
export async function OrderCount(params) {
	try {
		let res = await request({
			url: '/Order/OrderCount',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}

export async function Cashier(params) {
	try {
		let res = await request({
			url: '/Order/Cashier',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}

export async function Cashier_1(params) {
	try {
		let res = await request({
			url: '/Order/Cashier_1',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}

export async function delOrder(params) {
	try {
		let res = await request({
			url: '/Order/delOrder',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}
export async function teamOrder(params) {
	try {
		let res = await request({
			url: '/Order/teamOrder',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}

export async function GetOrderHistory(params) {
	try {
		let res = await request({
			url: '/Order/GetOrderHistory',
			method: "post",
			params
		})
		return res
	} catch (e) {
		return false
	}
}

export async function coupons(data) {
	try {
		let res = await request({
			url: '/Order/coupons',
			method: "post",
			data,
			isLogin: false
		})
		const { success, list } = res
		if (success) {
			let a = {};
			list.forEach(item => {
				a[item.id] = item
			})
			store.commit('user/SET_CARD', a)
		}
		return res;
	} catch (e) {
		return false
	}
}

export async function couponTotal(data) {
	try {
		let {success,data} = await request({
			url: '/Order/couponTotal',
			method: "post",
			data,
			isLogin: false
		})
		if (success) {
			console.log('data',data)
			store.commit('temporary/SET_OWN', data)
		}
		return data;
	} catch (e) {
		return false
	}
}